package com.situ.web.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.situ.web.dao.IBanjiDao;
import com.situ.web.entity.Banji;
import com.situ.web.util.JDBCUtil;

public class BanjiDaoImpl implements IBanjiDao{

	@Override
	public List<Banji> selectAll() {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		ArrayList<Banji> list = new ArrayList<Banji>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT `id`,`name` FROM banji";
			statement = connection.prepareStatement(sql);
			resultSet = statement.executeQuery();
			while(resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				Banji banji = new Banji(id, name);
				list.add(banji);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		
		return list;
	}

	@Override
	public int delete(int id) {
		int count = 0;
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "DELETE FROM banji WHERE id=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, id);
			count = statement.executeUpdate();
			System.out.println(count);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
		
		return count;
	}

	@Override
	public int insert(Banji banji) {
		int count = 0;
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO banji(`id`,`name`) VALUES(?,?)";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, banji.getId());
			statement.setString(2, banji.getName());
			count = statement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return count;
	}

	@Override
	public Banji selectId(int selectId) {
		Banji banji = null;
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT `id`,`name` FROM banji WHERE id=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, selectId);
			resultSet = statement.executeQuery();
			while(resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				banji = new Banji(id, name);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		return banji;
	}

	@Override
	public int update(Banji banji) {
		int count = 0;
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "UPDATE banji SET`name`=? WHERE `id`=?";
			statement = connection.prepareStatement(sql);
			statement.setString(1, banji.getName());
			statement.setInt(2, banji.getId());
			count = statement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, null);
		}
		
		return count;
	}

}
